//
//  HomeVC.swift
//  VIZ微博
//
//  Created by 位立志 on 16/4/7.
//  Copyright © 2016年 位立志. All rights reserved.
//

import UIKit
import SVProgressHUD

private let homeCellId = "homeCellId"

class HomeVC: VIZVistorTableVC {

    //模型数组
    //var statuList : [VIZHomeStatuse] = [VIZHomeStatuse]()
    
    //懒加载VIZHomeViewModel的对象
    private lazy var homeViewModel: VIZHomeViewModel = VIZHomeViewModel()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        //设置界面
        setUI()
        //加载数据
        loadData()
    }
    
    private func setUI() {
    
        if !isLogin {
            vistorView?.setVistorLogin("visitordiscover_feed_image_smallicon", labelText: "关注一些人,回到这里看看有什么惊喜", isHome: true)
            return
        }
     
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop",target: self, action: "push")
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch",target: self, action: "push")
        
        
        
        //注册cell
        tableView.registerClass(VIZStatueCell.self, forCellReuseIdentifier: homeCellId)
        //设置行高
//        tableView.rowHeight = 250
        /**
        设置行高自动计算
        */
        tableView.rowHeight = UITableViewAutomaticDimension
        /**
        设置预估行高
        */
        tableView.estimatedRowHeight = 300
    }
    
    private func loadData() {
        
        homeViewModel.loadHomeData { (isSuccess) -> () in
            
            if !isSuccess{
                SVProgressHUD.showErrorWithStatus("加载失败")
            }
            
            self.tableView.reloadData()
            
        }
        
    }
    
    

    private func push (){
        
        let tempVC = VIZTempVC()
        navigationController?.pushViewController(tempVC, animated: true)
        
    }

    
    
    
}

//数据源代理方法
extension HomeVC {
    
    //返回行数
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return homeViewModel.cellViewModelArray.count
    }
    
    //返回多少组
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    //设置单元格
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //let cell = tableView.dequeueReusableCellWithIdentifier(homeCellId)
        let cell = tableView.dequeueReusableCellWithIdentifier(homeCellId, forIndexPath: indexPath) as! VIZStatueCell
        //cell.textLabel?.text = homeViewModel.statuList[indexPath.row].user?.name
        //设置一个模型
        cell.cellViewModel = homeViewModel.cellViewModelArray[indexPath.row]
        
        return cell
    }
    
    
}








